package shark.picture.controller;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.util.Matrix;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class TestPDF1 {
    public static void main(String[] args) throws Exception {
        // 1. 加载原始PDF文档
        PDDocument doc = PDDocument.load(new File("E:/shark/需求文档/2025-04-29 直通车/文件汇总/业扩直通车-四类单据模板/受电工程竣工报验单模板.pdf"));
        String out = "E:/shark/需求文档/2025-04-29 直通车/文件汇总/业扩直通车-四类单据模板/受电工程竣工报验单1.pdf";
        PDPage page = doc.getPage(0);

        // 2. 加载中文字体
        PDType0Font font = loadFontFromResources(doc, "Font/ybst.ttf");

        // 3. 绘制坐标网格系统（调试用）
        try (PDPageContentStream stream = new PDPageContentStream(
                doc,
                page,
                PDPageContentStream.AppendMode.APPEND,
                true,
                true)) {

            // 设置网格样式
            stream.setStrokingColor(Color.RED);
            stream.setLineWidth(0.5f);

            // 获取页面尺寸
            float pageWidth = page.getMediaBox().getWidth();
            float pageHeight = page.getMediaBox().getHeight();

            // 绘制横向网格线（每50磅一条）
            for (float y = 0; y < pageHeight; y += 50) {
                stream.moveTo(0, y);
                stream.lineTo(pageWidth, y);
                stream.stroke();

                // 添加Y坐标标签
                stream.beginText();
                stream.setFont(font, 8);
                stream.setTextMatrix(Matrix.getTranslateInstance(5, y - 3));
                stream.showText(String.format("y=%.0f", y));
                stream.endText();
            }

            // 绘制纵向网格线
            for (float x = 0; x < pageWidth; x += 50) {
                stream.moveTo(x, 0);
                stream.lineTo(x, pageHeight);
                stream.stroke();

                // 添加X坐标标签
                stream.beginText();
                stream.setFont(font, 8);
                stream.setTextMatrix(Matrix.getTranslateInstance(x + 5, 10));
                stream.showText(String.format("x=%.0f", x));
                stream.endText();
            }

            // 在中心点添加标记（测试用）
            float centerX = pageWidth / 2;
            float centerY = pageHeight / 2;
            stream.setStrokingColor(Color.BLUE);
            stream.setLineWidth(1f);
            stream.moveTo(centerX - 20, centerY);
            stream.lineTo(centerX + 20, centerY);
            stream.moveTo(centerX, centerY - 20);
            stream.lineTo(centerX, centerY + 20);
            stream.stroke();

            // 添加中心点标签
            stream.beginText();
            stream.setFont(font, 10);
            stream.setTextMatrix(Matrix.getTranslateInstance(centerX - 30, centerY + 30));
            stream.showText(String.format("中心点(%.0f,%.0f)", centerX, centerY));
            stream.endText();
        }

        doc.save(out);
        doc.close();
        System.out.println("已生成带坐标网格的PDF，请用Acrobat查看具体位置");
    }

    private static PDType0Font loadFontFromResources(PDDocument doc, String resourcePath) throws IOException {
        try (InputStream fontStream = TestPDF1.class.getClassLoader().getResourceAsStream(resourcePath)) {
            if (fontStream == null) {
                throw new IOException("字体文件未找到: " + resourcePath);
            }
            return PDType0Font.load(doc, fontStream);
        }
    }
}
